package cn.zhuizikejithreeig.com.domeapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 圆形倒计时进度条
 *
 * @author xinyang
 */

public class ChengeSecondBackView extends View {


    private RectF mRectF;
    private Paint mPaint;
    private int add=0;
    private int width;
    private int height;
    /**
     * 画笔颜色
     */
    private int paintColor;
    /**
     * 进度条圆弧宽度
     */
    private int mStrokeWidth = 8;
    /**
     * 最大进度
     */
    private float mMaxProgress = 100;
    /**
     * 当前进度
     */
    private float mProgress = 0;


    public ChengeSecondBackView(Context context) {
        this(context, null);
    }

    public ChengeSecondBackView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChengeSecondBackView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mRectF = new RectF();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        width = this.getWidth();
        height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }

        //位置
        mRectF.left = mStrokeWidth / 2-add+100;
        mRectF.top = mStrokeWidth / 2-add+100;
        mRectF.right = width+add-100 - mStrokeWidth / 2;
        mRectF.bottom = height+add-100 - mStrokeWidth / 2;

        //设置画布为透明
        canvas.drawColor(Color.TRANSPARENT);

        //画个半透明的圆当背景
//        Color.parseColor("#33000000")
        mPaint.setColor(Color.TRANSPARENT);
        mPaint.setStyle(Paint.Style.STROKE);

        canvas.drawOval(mRectF, mPaint);


        //设置画笔
        mPaint.setAntiAlias(true);
        mPaint.setAlpha((int) (100-add));
        mPaint.setColor(paintColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);


        canvas.drawArc(mRectF, -90, 360 - (mProgress / mMaxProgress) * 360, false, mPaint);
    }

    /**
     * 设置画笔颜色
     *
     * @param color
     */
    public void setPaintColor(String color) {
        paintColor = Color.parseColor(color);
    }

    /**
     * 设置到最大进度的时间
     */
    public void addRedio(final int madd) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                add = madd;


                ChengeSecondBackView.this.postInvalidate();
            }
        }).start();
    }


}
